package com.snowm.springboot.config;

import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class JettyConfig {

	@Value("${server.jetty.minThreads:10}")
	private int jettyMinThreads;
	
	@Value("${server.jetty.maxThreads:200}")
	private int jettyMaxThreads;

	@Value("${server.jetty.idleTimeout:60000}")
	private int jettyIdleTimeout;

	@Bean
	public EmbeddedServletContainerCustomizer jettyCustomizer() {
		return (container) -> {
			if (container instanceof JettyEmbeddedServletContainerFactory) {
				JettyEmbeddedServletContainerFactory jettyContainer = ((JettyEmbeddedServletContainerFactory) container);
				jettyContainer.setThreadPool(new QueuedThreadPool(jettyMaxThreads, jettyMinThreads, jettyIdleTimeout));
			}
		};
	}
	
}
